package com.xteam.youin_supplier.fragment;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonSyntaxException;
import com.xteam.youin_supplier.MainActivity;
import com.xteam.youin_supplier.R;
import com.xteam.youin_supplier.config.RequestAddr;
import com.xteam.youin_supplier.model.BaseModel;
import com.xteam.youin_supplier.model.TruckDetail;
import com.xteam.youin_supplier.model.TruckList;
import com.xteam.youin_supplier.util.http.AjaxCallBack;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;

/**
 * 车辆加油详情
 */
@EFragment(R.layout.fragment_car_oil_detail)
public class CarOilDetailFragment extends BaseFragment {
    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @ViewById(R.id.listview)
    RecyclerView listView;

    @FragmentArg("order_id")
    int order_id;

    @ViewById(R.id.car_name_content)
    TextView carName;
    @ViewById(R.id.license_num_content)
    TextView licenseNum;
    @ViewById(R.id.add_oil_num_content)
    TextView oilNum;
    @ViewById(R.id.price_single_content)
    TextView priceSigle;
    @ViewById(R.id.month_num_content)
    TextView monthNum;
    @ViewById(R.id.month_total_num_content)
    TextView totalNum;

    @AfterViews
    @Override
    void initView() {
        backBtn.setVisibility(View.VISIBLE);
        topTitle.setText(getResources().getString(R.string.add_oil_detail_title));

        loadData();
    }

    private void loadData() {
        requestParams = "order_id="+order_id;
        requestUrl = RequestAddr.addoilDetail + requestParams;
        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                dealData(object);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    TruckDetail truckDetail;
    private void dealData(Object object) {
        try {
            truckDetail = gson.fromJson(object.toString(), TruckDetail.class);
        } catch (JsonSyntaxException e) {
            baseModel = gson.fromJson(object.toString(), BaseModel.class);
            Toast.makeText(getActivity(), baseModel.getMsg(), Toast.LENGTH_SHORT).show();
            return;
        }

         carName.setText(truckDetail.getData().getName());
         licenseNum.setText(truckDetail.getData().getCar_num());
         oilNum.setText(truckDetail.getData().getQty());
         priceSigle.setText(truckDetail.getData().getPrice());
//         monthNum;
         totalNum.setText(truckDetail.getData().getTotal_price());

    }

    @Click({R.id.back_btn, R.id.edit_btn})
    @Override
    void Click(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstance().getSupportFragmentManager().popBackStack();
                break;
        }
    }
}
